/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ec.socialfun.validadores;

import com.ec.socialfun.servicios.UsuarioFacade;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.jboss.logging.Logger;

/**
 *
 * @author hp
 */
@ManagedBean
@RequestScoped
public class MailValidador implements Validator {

    private static final Logger LOG = Logger.getLogger(MailValidador.class.getName());
    @EJB
    UsuarioFacade usuarioFacade;
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
            + "[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*"
            + "(\\.[A-Za-z]{2,})$";
    private Pattern pattern;
    private Matcher matcher;

    @Override
    public void validate(FacesContext fc, UIComponent uic, Object obj) throws ValidatorException {

        pattern = Pattern.compile(EMAIL_PATTERN);
        String mail = obj.toString();
        LOG.info("mail = " + mail);
        LOG.info("usuarioFacade = " + usuarioFacade);

        matcher = pattern.matcher(mail);

        LOG.info("validate 1 ");
        if (!matcher.matches()) {
            LOG.info("validate 1.1 ");
            FacesMessage msg =
                    new FacesMessage("El mail ingresado no tiene la estructura correcta",
                    "");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);

        }

        if (mail.length() > 50) {
            FacesMessage msg = new FacesMessage("El mail ingresado debe poseer maximo 50 caracteres",
                    "");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }

        if (!usuarioFacade.verificaMailNoRegistrado(mail)) {
            FacesMessage msg = new FacesMessage("El mail ingresado ya se encuentra registrado",
                    "");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }

    public String validarMail(String mailUsuario) {
        
        pattern = Pattern.compile(EMAIL_PATTERN);
        String mail = mailUsuario;
        LOG.info("mail = " + mail);
        LOG.info("usuarioFacade = " + usuarioFacade);
        
        if (mail == null || mail.trim().length() == 0) {
            return "El Mail es un campo requerido";
        }

        matcher = pattern.matcher(mail);

        LOG.info("validate 1 ");
        if (!matcher.matches()) {
            return "El mail ingresado no tiene la estructura correcta";
        }

        if (mail.length() > 50) {
            return "El mail ingresado debe poseer maximo 50 caracteres";
        }

        if (!usuarioFacade.verificaMailNoRegistrado(mail)) {
            return "El mail ingresado ya se encuentra registrado";
        }

        return null;
    }
}
